import java.io.*;
import java.util.Arrays;

public class IOTest {
	public static void main1(String[] args) throws IOException {
		Writer wr=new FileWriter("字符流.txt",true);
		wr.write(97);
		wr.write(98);
		wr.write(99);
		wr.write(100);
		wr.write("你好");
		wr.write(new char [] {'a','b','c','d'});
		wr.flush();
		wr.close();
		Reader reader=new FileReader("字符流.txt");
		int i =-1;
		while((i= reader.read())!=-1) {
			System.out.println((char) i);
		}
	}
	public static void main2(String[] args) throws IOException {
		File file=new File("D:\\IdeaCode\\javacode\\Education\\字符流.txt");
		//这里的关系是，先看这个目录在不在，不在的话，先进行创建目录
		//目录创建好了，在进行创建文件
		String fileparent=file.getParent();
		File file1=new File(fileparent,"字符流1.txt");
		file1.createNewFile();

		//Writer writer=new FileWriter("字符流1.txt");
		//文本文档之间赋值的两种方式，一个是借助缓冲区的，一个是直接进行写入
		char [] chars=new char[10];
		Reader reader=new FileReader("字符流.txt");
		int len;
		int k=0;
		while((len= reader.read())!=-1) {
			chars[k++]=(char) len;
			//writer.write((char) len);
		}
		System.out.println(Arrays.toString(chars));


		Writer writer=new FileWriter("字符流1.txt");
		writer.write(chars);
		writer.flush();
	}

	public static void main(String[] args) throws IOException {
		String srcPath = "F:\\2024河南师范大学JavaEE课程课件\\myjavapros\\MyFirstPro\\src\\cn\\neusoft\\demo\\Demo09.java";
		File srcFile = new File(srcPath);

		String targetPath = "E:\\abc\\aa\\code";
		String fileName = srcFile.getName(); //获取源文件的文件名
		File targetFile = new File(targetPath,fileName);

		//判断父路径是否存在  不存要创建
		File parentFile = targetFile.getParentFile();
		if (!parentFile.exists()){
			parentFile.mkdirs();
		}

		//缓冲流的写法
		Reader reader = null;
		BufferedReader br=new BufferedReader(new FileReader(srcFile));
		BufferedWriter bw=new BufferedWriter(new FileWriter(targetFile));
		Writer writer=null;
		try {
			//创建输入流


			//读写操作  复制
//            int i;
//            while ((i = reader.read())!=-1){
//                writer.write(i);
//            }

			//读到缓冲区  将缓冲区的内容写出去
//			char[] buf = new char[1024];
//			int len;
//			while ((len = reader.read(buf))>0){
//				writer.write(buf,0,len);
//			}
			String buffer=null;
			while((buffer=br.readLine())!=null) {
				bw.write(buffer);
				bw.newLine();
				bw.flush();
			}

			System.out.println("复制完毕");

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//关闭流
			if (reader!=null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if(writer!=null){
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
